﻿using PagedList;
using SelfMadeMillionaire.Core;
using SelfMadeMillionaire.Core.Entities;
using SelfMadeMillionaire.Core.Enums;
using SelfMadeMillionaire.Core.Services;
using SelfMadeMillionaire.Web.Infrastructure.Controllers;
using SelfMadeMillionaire.Web.Infrastructure.Formatters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using SelfMadeMillionaire.Web.Infrastructure.ViewModels.Promotion;

namespace SelfMadeMillionaire.Web.Infrastructure.Controllers.Admin
{
    public class PromotionController : AdminController
    {

        IPromotionService _promotionService;

        public PromotionController(IPromotionService promotionService)
		{
            _promotionService = promotionService;
		}

        class PromotionFilterSave
        {
            public int? Page;
            public string Sort;
        }

        public ActionResult Index(int? page, string sort)
        {
            if (Session["UsePromotionFilter"] != null && Session["PromotionFilter"] != null)
            {
                Session["UsePromotionFilter"] = null;
                var filter = Session["PromotionFilter"] as PromotionFilterSave;

                page = filter.Page;
                sort = filter.Sort;
            }
            else
            {
                Session["PromotionFilter"] = new PromotionFilterSave()
                {
                    Page = page,
                    Sort = sort
                };
            }

            int pageIndex = page ?? 1;
            int pageSize = 10;

            var model = new PromotionListViewModel()
            {
                SortOrder = sort
            };

            IEnumerable<Promotion> list = _promotionService.GetAll();
            if (!string.IsNullOrEmpty(sort))
            {
                string[] sortArr = sort.Split(',');
                if (sortArr[1] == "0")
                {

                    switch (sortArr[0])
                    {
                        case "0":
                            list = list.OrderBy(i => i.Name);
                            break;
                        case "1":
                            list = list.OrderBy(i => i.Discount);
                            break;
                        case "2":
                            list = list.OrderBy(i => !i.IsPercent);
                            break;
                        case "3":
                            list = list.OrderBy(i => i.PromoCode);
                            break;
                        case "4":
                            list = list.OrderBy(i => i.EffectiveDate);
                            break;
                        case "5":
                            list = list.OrderBy(i => i.ExpirationDate);
                            break;
                        case "6":
                            list = list.OrderBy(i => i.ProductText);
                            break;
                        case "7":
                            list = list.OrderBy(i => i.Status);
                            break;
                    }

                }
                else
                {
                    switch (sortArr[0])
                    {
                        case "0":
                            list = list.OrderByDescending(i => i.Name);
                            break;
                        case "1":
                            list = list.OrderByDescending(i => i.Discount);
                            break;
                        case "2":
                            list = list.OrderByDescending(i => !i.IsPercent);
                            break;
                        case "3":
                            list = list.OrderByDescending(i => i.PromoCode);
                            break;
                        case "4":
                            list = list.OrderByDescending(i => i.EffectiveDate);
                            break;
                        case "5":
                            list = list.OrderByDescending(i => i.ExpirationDate);
                            break;
                        case "6":
                            list = list.OrderByDescending(i => i.ProductText);
                            break;
                        case "7":
                            list = list.OrderByDescending(i => i.Status);
                            break;
                    }
                }
            }
            model.Promotions = list.ToList().ToPagedList(pageIndex, pageSize);
            var promoEnumDescriptions = from PromotionProduct n in Enum.GetValues(typeof(PromotionProduct))
                                     select new { Value = (int)n, Text = GetEnumDescription(n) };
            foreach (var promo in model.Promotions)
            {
                promo.ProductText = promoEnumDescriptions.FirstOrDefault(x=>x.Value==promo.Product).Text;
            }
                        

            return View(model);
        }

        public ActionResult AddOrEditPromo(int id = 0)
        {
            var model = new PromotionEditViewModel();
           
            if (id != 0)
            {
                var promo = _promotionService.Get(id);
                model.ID = promo.ID;
                model.Name = promo.Name;
                model.Discount = promo.Discount;
                model.IsPercent = promo.IsPercent;
                model.PromoCode = promo.PromoCode;
                model.EffectiveDate = promo.EffectiveDate;
                model.ExpirationDate = promo.ExpirationDate;
                model.Product = promo.Product;
                model.Status = promo.Status;
            }
            else
            {
                model.EffectiveDate = DateTime.Today;
                model.ExpirationDate = DateTime.Today;
                model.IsPercent = true;
            }
            
            var promoEnumDescriptions = from PromotionProduct n in Enum.GetValues(typeof(PromotionProduct))
                                        select new { Value = (int)n, Text = GetEnumDescription(n)};

            IEnumerable<SelectListItem> a = promoEnumDescriptions.Select(c => new SelectListItem()
            {
                Text = c.Text,
                Value = c.Value.ToString(),
                Selected = c.Value == model.Product
            }).ToList();

            model.ProductList = a;

            Session["UsePromotionFilter"] = true;
            return View(model);
        }
        
        [HttpPost]
        public ActionResult AddOrEditPromo(PromotionEditViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return RedirectToAction("AddOrEditPromo", model.ID);
            }

            var promotion = new Promotion()
            {
                Name = model.Name,
                Discount = model.Discount,
                IsPercent = model.IsPercent,
                PromoCode = model.PromoCode,
                EffectiveDate = model.EffectiveDate,
                ExpirationDate = model.ExpirationDate,
                Product = model.Product,
                Status = model.Status
            };

            if (model.ID == 0)
            {
                _promotionService.Add(promotion);
            }
            else
            {
                promotion.ID = model.ID;
                _promotionService.Update(promotion);
            }

            return RedirectToAction("Index");
        }

        [HttpPost]
        public void Delete(int id)
        {
            _promotionService.Remove(id);
        }

        [HttpPost]
        public void Stop(int id)
        {
            Promotion promo = _promotionService.Get(id);
            if (promo != null)
            {
                promo.ExpirationDate = DateTime.Today.Date.AddDays(-1);
                _promotionService.Update(promo);
            }
        }
    }
}
